package sketch.generator;

public abstract class CompositeGenerator<T> extends Generator<T> {
	
	//the iterator for going through the whole children nodes
	protected IIterationLink iterator;

	public CompositeGenerator() {
		super();
	}
	
	public IIterationLink getIterator() {
		if(iterator == null) {
			iterator = getInitializedIterator();
		}
		return iterator;
	}


	@Override
	public boolean hasNext() {
		return getIterator().hasNext();
	}
	
	
	@Override
	protected T generateNext() {
		if(!getIterator().hasNext()) {
			throw new IterationException("There is no more element" +
					" to create in composite generator!");
		}
		getIterator().next();
		return generateCurrent();
	}
	
	@Override
	public void reset() {
		super.reset();
		iterator = null;
		//re-grasp the iterator
		getIterator().reset();
	}
	
	/**
	 * Assemble the return value from current generated values
	 * */
	public abstract T generateCurrent();
	
	/**
	 * Initialize the iterator
	 * */
	public abstract IIterationLink getInitializedIterator();

}
